﻿
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

namespace SNSHelper.Common
{
    /// <summary>
    /// 解压缩类
    /// </summary>
    public static class UnZip
    {
        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <param name="zipFilePath">压缩文件路径</param>
        /// <param name="unZipFilePatah">解压缩文件路径</param>
        public static void UnZipFile(string zipFilePath, string unZipFilePatah)
        {
            byte[] buffer = new byte[2048];

            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry zipEntry = null;
                while ((zipEntry = zipStream.GetNextEntry()) != null)
                {
                    string fileName = Path.GetFileName(zipEntry.Name);
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (zipEntry.CompressedSize == 0)
                            break;
                        using (FileStream stream = File.Create(unZipFilePatah + fileName))
                        {
                            while (true)
                            {
                                int size = zipStream.Read(buffer, 0, buffer.Length);
                                if (size > 0)
                                {
                                    stream.Write(buffer, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <param name="zipFilePath">压缩文件路径</param>
        /// <param name="unZipFilePatah">解压缩文件路径</param>
        public static void UnZipStringToFile(string zipData, string unZipFilePatah)
        {
            byte[] buffer = new byte[2048];

            using (MemoryStream memStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(zipData)))
            {
                using (ZipInputStream zipStream = new ZipInputStream(memStream))
                {
                    ZipEntry zipEntry = null;
                    while ((zipEntry = zipStream.GetNextEntry()) != null)
                    {
                        string fileName = Path.GetFileName(zipEntry.Name);
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            if (zipEntry.CompressedSize == 0)
                                break;

                            using (FileStream stream = File.Create(unZipFilePatah + fileName))
                            {
                                while (true)
                                {
                                    int size = zipStream.Read(buffer, 0, buffer.Length);
                                    if (size > 0)
                                    {
                                        stream.Write(buffer, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                //stream.CopyTo(zipStream);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 解压缩目录
        /// </summary>
        /// <param name="zipDirectoryPath">压缩目录路径</param>
        /// <param name="unZipDirecotyPath">解压缩目录路径</param>
        public static void UnZipDirectory(string zipDirectoryPath, string unZipDirecotyPath)
        {
            byte[] buffer = new byte[2048];

            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath)))
            {
                ZipEntry zipEntry = null;
                while ((zipEntry = zipStream.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(zipEntry.Name);
                    string fileName = Path.GetFileName(zipEntry.Name);

                    if (!string.IsNullOrEmpty(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (zipEntry.CompressedSize == 0)
                            break;
                        if (zipEntry.IsDirectory)
                        {
                            directoryName = Path.GetDirectoryName(unZipDirecotyPath + zipEntry.Name);
                            Directory.CreateDirectory(directoryName);
                        }

                        using (FileStream stream = File.Create(unZipDirecotyPath + zipEntry.Name))
                        {
                            while (true)
                            {
                                int size = zipStream.Read(buffer, 0, buffer.Length);
                                if (size > 0)
                                {
                                    stream.Write(buffer, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 解压缩目录
        /// </summary>
        /// <param name="zipDirectoryPath">压缩目录路径</param>
        /// <param name="unZipDirecotyPath">解压缩目录路径</param>
        public static bool UnZipFiles(Dictionary<string, string> unZipFiles, string unZipPath)
        {
            byte[] buffer = new byte[2048];
            int count = 0;

            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(unZipPath)))
            {
                ZipEntry zipEntry = null;
                while ((zipEntry = zipStream.GetNextEntry()) != null)
                {

                    if (count == unZipFiles.Count)
                    {
                        break;
                    }

                    if (unZipFiles.Keys.Contains(zipEntry.Name) == false)
                    {
                        continue;
                    }

                    using (FileStream stream = File.Create(unZipFiles[zipEntry.Name]))
                    {
                        zipStream.CopyTo(stream);
                        ++count;
                    }
                }
            }

            if (count != unZipFiles.Count)
            {
                foreach (KeyValuePair<string, string> var in unZipFiles)
                {
                    if (File.Exists(var.Value) == false)
                    {
                        using(FileStream stream = File.Create(var.Value))
                        {
                        }
                    }
                }

                return true;
            }

            return true;
        }
    }
}
